/**
 * Created by Administrator on 2016/11/7.
 */
var _interface = require('../../models/interface/interface');
var _module = require("../../models/scjg/scjg");
var fs = require('fs');

// 最近三十天与今年 格式：new Date() to 2012-03-21
function dateToGs(type) {
    var d       = new Date();
    // 当前日期
    var month   = d.getMonth() + 1 > 9 ? (d.getMonth() + 1 ) : '0' + (d.getMonth() + 1);
    var day     = d.getDate() > 9 ? d.getDate() : '0' + d.getDate();
    var endDate = d.getFullYear() + '-' + month + '-' + day;

    // 三十天前日期
    var bDi       = d.setDate(d.getDate() - 30);
    var bD        = new Date(bDi);
    var bMonth    = bD.getMonth() + 1 > 9 ? (bD.getMonth() + 1 ) : '0' + (bD.getMonth() + 1);
    var bDay      = bD.getDate() > 9 ? bD.getDate() : '0' + bD.getDate();
    var beginDate = bD.getFullYear() + '-' + bMonth + '-' + bDay;

    if (type == 'month') {
        var time = {
            beginDate: beginDate,
            endDate  : endDate
        }
    }

    if (type == 'year') {
        var time = {
            beginDate: d.getFullYear() + '-01-01',
            endDate  : endDate
        }
    }

    if (type == '') {
        var time = {
            beginDate: '',
            endDate  : ''
        }
    }

    return time;
}

// 行业分类
exports.vocationList = function (req, res, next) {
    _interface.getVocationList({
        pageIndex : req.body.pageIndex || 1,
        pageRowNum: req.body.pageRowNum || 10
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 商事主体列表信息
exports.custList = function (req, res, next) {
    var type = req.body.type;
    if (type == 'oths') {
        var DateBegin = req.body.publishDateBegin || '',
            DateEnd   = req.body.publishDateEnd || '';
    } else {
        var DateBegin = dateToGs(type).beginDate || '',
            DateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getCustList({
        status          : req.body.status || '',
        publishDateBegin: DateBegin,
        publishDateEnd  : DateEnd,
        regCapitalBegin : req.body.regCapitalBegin || '',
        regCapitalEnd   : req.body.regCapitalEnd || '',
        pageRowNum      : req.body.pageRowNum || '',
        pageIndex       : req.body.pageIndex || 1,
        industry        : req.body.industry || '',
        regNumLike      : req.body.regNumLike || '',
        credNumLike     : req.body.credNumLike || '',
        custNameLike    : req.body.custNameLike || '',
        params          : req.body.params || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 商事主体信息数量统计或（首页托管商事数量）
exports.custCount = function (req, res, next) {
    var type = req.body.type || '';
    _interface.getCustCount({
        status          : req.body.status || '',
        publishDateBegin: dateToGs(type).beginDate || '',
        publishDateEnd  : dateToGs(type).endDate || '',
        regCapitalBegin : req.body.regCapitalBegin || '',
        regCapitalEnd   : req.body.regCapitalEnd || '',
        nameLike        : req.body.nameLike || '',
        industry        : req.body.industry || ''
    }).done(function (data){
        res.send(JSON.parse(data));
    });
}

// 处罚公示列表
exports.punishQuery = function (req, res, next) {
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.publishDateBegin || '',
            publishDateEnd   = req.body.publishDateEnd || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getPunishQuery({
        beginDate : publishDateBegin || '',
        endDate   : publishDateEnd || '',
        pageRowNum: req.body.pageRowNum || 10,
        pageIndex : req.body.pageIndex || 1,
        custId    : req.body.custId || '',
        regNum    :  req.body.regNum || '',
        custName  :  req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 处罚公示统计总条数
exports.punishCount = function (req, res, next) {
    var type = req.body.type || '';
    _interface.getPunishCount({
        beginDate: dateToGs(type).beginDate || '',
        endDate  : dateToGs(type).endDate || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 荣誉信息列表
exports.honorListQuery = function (req, res, next) {
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.beginDate || '',
            publishDateEnd   = req.body.endDate || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getHonorListQuery({
        beginDate : publishDateBegin,
        endDate   : publishDateEnd,
        pageRowNum: req.body.pageRowNum || 10,
        pageIndex : req.body.pageIndex || 1,
        regNum    : req.body.regNum || '',
        custNameLike    : req.body.custNameLike || '',
        custName    : req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 荣誉信息统计
exports.honorCount = function (req, res, next) {
    var type = req.body.type || '';
    _interface.getHonorCount({
        beginDate: dateToGs(type).beginDate || '',
        endDate  : dateToGs(type).endDate || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 异常名录列表
exports.exceptionDirList = function (req, res, next) {
    console.log('=====================================');
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.publishDateBegin || '',
            publishDateEnd   = req.body.publishDateEnd || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getExceptionDirList({
        beginDate : publishDateBegin || '',
        endDate   : publishDateEnd || '',
        pageRowNum: req.body.pageRowNum ||"",
        pageIndex : req.body.pageIndex || 1,
        custId    : req.body.custId || '',
        custName  : req.body.custName || '',
        regNum    : req.body.regNum || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 异常名录统计列表
exports.exceptionDirCountList = function (req, res, next) {
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.publishDateBegin || '',
            publishDateEnd   = req.body.publishDateEnd || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getExceptionDirCountList({
        beginDate : publishDateBegin || '',
        endDate   : publishDateEnd || '',
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custName  :req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
};
// 严重违法列表
exports.illegal               = function (req, res, next) {
    _interface.getIllegal({
        pageRowNum: req.body.pageRowNum || '',
        currPage  : req.body.currPage || '',
        totalPage : req.body.totalPage || '',
        totalRow  : req.body.totalRow || '',
        pageIndex : req.body.pageIndex || '',
        custName  :req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
};

// 异常名录统计总条数
exports.exceptionDirCount = function (req, res, next) {
    var type = req.body.type || '';
    _interface.getExceptionDirCount({
        beginDate: dateToGs(type).beginDate || '',
        endDate  : dateToGs(type).endDate || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 检验检测列表
exports.custDayCheckList = function (req, res, next) {
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.publishDateBegin || '',
            publishDateEnd   = req.body.publishDateEnd || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getCustDayCheckList({
        pageRowNum: req.body.pageRowNum || "",
        pageIndex : req.body.pageIndex || "",
        beginDate:publishDateBegin,
        endDate:publishDateEnd,
        custNameLike  :req.body.custNameLike || ''
    }).done(function (data) {
        console.log(data);
        res.send(JSON.parse(data));
    });
}
// 检验检测列表左侧列表
exports.check= function (req, res, next) {
    _interface.getCheck({
        pageRowNum: req.body.pageRowNum || 4,
        pageIndex : req.body.pageIndex || 1,
        custName  :req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}
// 检验检测列表右侧
exports.check= function (req, res, next) {
    _interface.getCheck({
        pageRowNum: req.body.pageRowNum || 4,
        pageIndex : req.body.pageIndex || 1,
        custName  :req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 检验检测统计---废弃
// exports.dayCheckCount = function (req, res, next) {
//     var type = req.body.type || '';
//     _interface.getDayCheckCount({
//         beginDate: dateToGs(type).beginDate || '',
//         endDate  : dateToGs(type).endDate || ''
//     }).done(function (data) {
//         res.send(JSON.parse(data));
//     });
// }

// 首页网站图片
exports.netLogoList = function (req, res, next) {
    _interface.getNetLogoList({
        netType   : req.body.netType || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 小百科
exports.baike    = function (req, res, next) {
    _interface.getBaike({
        noticeType: req.body.noticeType || '',
        seq       : req.body.seq || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
};
// 司法助手
exports.judicial = function (req, res, next) {
    _interface.getJudicial({
        regNum: req.body.regNum || '',
        custNameLike: req.body.custNameLike || '',
        beginDate: req.body.beginDate || '',
        endDate: req.body.endDate || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
};

// 搜索服务次数统计
exports.serCount = function (req, res, next) {
    _interface.getSerCount({}).done(function (data) {
        res.send(JSON.parse(data));
    });
};

// 搜索服务次数统计-添加纪录
exports.serCountAdd = function (req, res, next) {
    _interface.getSerCountAdd({
        selText: req.body.selText || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}


// 开发区行业分布情况（饼状图接口）
exports.industryList     = function (req, res, next) {
    _interface.getIndustryList({
        selText: req.body.selText || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}
// 开发区近5年企业登记状况（柱状图接口）
exports.custRegSituate   = function (req, res, next) {
    _interface.getCustRegSituate({
        selText: req.body.selText || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}
// 开发区资本规模分布（柱状图接口）
exports.countCustCapital = function (req, res, next) {
    _interface.getCountCustCapital({
        selText: req.body.selText || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 信息发布轮循图片和首页网站图片
exports.noticePhoto = function (req, res, next) {
    _interface.getNoticePhoto({
        firstSeq: req.body.firstSeq || 7,
        bussType: req.body.bussType || 1
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 许可信息
exports.license = function (req, res, next) {
    _interface.getLicense({
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custNameLike : req.body.custNameLike || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 认证信息
exports.identification = function (req, res, next) {
    _interface.getIdentification({
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custNameLike : req.body.custNameLike || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 标准专利
exports.pantent = function (req, res, next) {
    _interface.getPantent({
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custNameLike : req.body.custNameLike || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 评级评测
exports.custGrade = function (req, res, next) {
    _interface.getCustGrade({
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custNameLike : req.body.custNameLike || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 担保抵押
exports.pledge = function (req, res, next) {
    _interface.getPledge({
        pageRowNum: req.body.pageRowNum || '',
        pageIndex : req.body.pageIndex || '',
        custNameLike : req.body.custNameLike || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

exports.dayCheckCount = function (req, res, next) {
    var type = req.body.type || '';
    _interface.custDayCheck({
        beginDate: dateToGs(type).beginDate || '',
        endDate  : dateToGs(type).endDate || '',
        custName  :req.body.custName || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    });
}

// 网站累计访问量人次统计
exports.visitCount = function (req,res,next) {
    _interface.getVisitCount({})
        .done(function (data) {
            res.send(JSON.parse(data));
        })
}

// 网站累计访问量新增
exports.addVisitCount = function (req,res,next) {
    var getClientIP = function() {
        var ipAddress;
        var headers = req.headers;
        var forwardedIpsStr = headers['x-real-ip'] || headers['x-forwarded-for'];
        forwardedIpsStr ? ipAddress = forwardedIpsStr : ipAddress = null;
        if (!ipAddress) {
            ipAddress = req.connection.remoteAddress;
        }
        return ipAddress;
    }
    _interface.getAddVisitCount({
        ipAddrdss:getClientIP() || '',
        broType:req.body.broType || ''
    }).done(function (data) {
        res.send(JSON.parse(data));
    })
}

// 网站累计访问量人次统计
exports.permissionCount = function (req,res,next) {
    var type = req.body.type;
    if (type == 'oths') {
        var publishDateBegin = req.body.publishDateBegin || '',
            publishDateEnd   = req.body.publishDateEnd || '';
    } else {
        var publishDateBegin = dateToGs(type).beginDate || '',
            publishDateEnd   = dateToGs(type).endDate || '';
    }
    _interface.getPermissionCount({
        "beginDate":publishDateBegin,
        "endDate":publishDateEnd,
        "regNum":"",
        "custNameLike":"",
        "credNum":""
    }).done(function (data) {
        res.send(JSON.parse(data));
})
}
